package com.example.demo.springboot.mqservice.provider;

import com.example.demo.springboot.mqclient.consumer.MqConsumerConstant;
import com.example.demo.springboot.mqservice.entity.MqReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ProviderController:这里直接模拟MQ发送消息,当然也可以在 <a href="http://127.0.0.1:15672/#/">管理控制台</a>  发送消息
 *
 * @author zhangxiaoxiang
 * @since 2023/10/1
 */
@Slf4j
@RequestMapping
@RestController("/provider")
@RequiredArgsConstructor
public class ProviderController {


    private final RabbitTemplate rabbitTemplate;

    @PostMapping("/one")
    public void test1(@RequestBody MqReq mqReq) {
        // 1对1模式
        rabbitTemplate.convertAndSend("outbound.calllog.one.queue", mqReq.getData());
    }

    @PostMapping("/work")
    public void test2(@RequestBody MqReq mqReq) {
        rabbitTemplate.convertAndSend("call.work.queue", mqReq.getData());
    }


    @PostMapping("/fanout")
    public void test3(@RequestBody MqReq mqReq) {
        // 不指定路由键,直接往交换机投递消息即可
        rabbitTemplate.convertAndSend(MqConsumerConstant.FANOUT_CALLLOG_EXCHANGE, "", mqReq.getData());
    }

    @PostMapping("/direct")
    public void test4(@RequestBody MqReq mqReq) {
        // 路由模式[这里是路由键固定,精准投递型,1对1,缺点是路由key可能太多,不好维护]
        rabbitTemplate.convertAndSend(MqConsumerConstant.DIRECT_CALLLOG_EXCHANGE, MqConsumerConstant.DIRECT_CRM_CALLLOG_KEY,"给CRM"+mqReq.data);
        // rabbitTemplate.convertAndSend(MqConsumerConstant.DIRECT_CALLLOG_EXCHANGE, MqConsumerConstant.DIRECT_ZHIYU_CALLLOG_KEY, "给ZHIYU"+mqReq.data);
    }

    @PostMapping("/topics")
    public void test5(@RequestBody MqReq mqReq) {
        // 路由模式 callback.crm [路由键就是模糊匹配]
        rabbitTemplate.convertAndSend(MqConsumerConstant.TOPIC_CALLLOG_EXCHANGE, "callback.erp.test", mqReq.data);
        // rabbitTemplate.convertAndSend(MqConsumerConstant.TOPIC_CALLLOG_EXCHANGE, "callback.zhiyu.test", mqReq.data);
    }
}
